package test;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;

import oopdbms.storage.model.ClassInfo;


public class testObjectStream {
	public void writeObject(){
		ClassInfo ci = new ClassInfo();
		System.out.println(ci);
		byte[] objByte = null;
		try {
			RandomAccessFile raf = new RandomAccessFile(new File("object.dat"),"rw");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(baos);
			out.writeObject(ci);
			objByte = baos.toByteArray();
			raf.seek(512);
			System.out.println("write start position is "+raf.getFilePointer());
			raf.write(objByte);
			System.out.println("write end position is "+raf.getFilePointer());
			System.out.println("Byte Array's length:"+objByte.length);
			out.close();
			baos.close();
			raf.setLength(raf.getFilePointer());
			raf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void ReadObject(){
		ClassInfo ci = null;
		byte[] objByte = new byte[100];
		try {
			RandomAccessFile raf = new RandomAccessFile(new File("object.dat"),"r");
			raf.seek(512);
			raf.read(objByte);
			ByteArrayInputStream bais = new ByteArrayInputStream(objByte);
			ObjectInputStream in = new ObjectInputStream(bais);
			ci = (ClassInfo)in.readObject();
			System.out.println("read the object,time is "+ci);
			in.close();
			bais.close();
			raf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		testObjectStream tos = new testObjectStream();
		tos.writeObject();
		tos.ReadObject();
	}
}
